const Controller = require('egg').Controller

class CollectionController extends Controller {
  /* 前台 */
  // 根据用户找收藏夹(author)
  async getCollectionsByOwner() {
    try {
      const { ctx } = this
      const { author } = ctx.request.body
      const collections = await ctx.model.Collection.findAll({
        where: {
          owner_id: author
        }
      })
      ctx.body = {
        code: '0',
        collections: collections,
        desc: '获取成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '获取失败',
        err: error
      }
    }
  }

  // 获取收藏夹中文章
  async getArticleByCollection() {
    try {
      const { ctx } = this
      const { id } = ctx.request.body
      const collections = await ctx.model.Collection.findAll({
        where: {
          id
        },
        include: [{
          model: article,
          attributes:['title','picture', 'view', 'like', 'created_at']
        }]
      })
      ctx.body = {
        code: '0',
        collections: collections,
        desc: '获取成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '获取失败',
        err: error
      }
    }
  }

  // 新建收藏夹
  async createCollection() {
    try {
      const { ctx } = this
      const { title, description, is_public, owner_id } = ctx.request.body
      await ctx.model.Collection.create({
        title,
        description,
        public: is_public,
        owner_id
      })
      ctx.body = {
        code: '0',
        desc: '新建成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '新建失败',
        err: error
      }
    }
  }

  // 添加文章到收藏夹
  async addToCollection() {
    try {
      const { ctx } = this
      const { article_id, collection_id } = ctx.request.body
      const col = await ctx.model.Collection.findByPk(collection_id)
      await col.addArticle(article_id)
      ctx.body = {
        code: '0',
        desc: '添加成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '添加失败',
        err: error
      }
    }
  }

  // 收藏夹中是否有该文章
  async collectionHasArticle() {
    try {
      const { ctx } = this
      const { article_id, collection_id } = ctx.request.body
      const col = await ctx.model.Collection.findByPk(collection_id)
      const detail = await col.hasArticle(Number(article_id))
      ctx.body = {
        code: '0',
        detail: detail,
        desc: '找到文章'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '未找到文章',
        err: error
      }
    }
  }

  // 删除收藏夹中文章
  async deleteFromCollection() {
    try {
      const { ctx } = this
      const { article_id, collection_id } = ctx.request.body
      const col = await ctx.model.Collection.findByPk(collection_id)
      await col.removeArticle(Number(article_id))
      ctx.body = {
        code: '0',
        desc: '删除成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '删除失败',
        err: error
      }
    }
  }

  // 清空收藏夹
  async clearCollection() {
    try {
      const { ctx } = this
      const { collection_id } = ctx.request.body
      const col = await ctx.model.Collection.findByPk(collection_id)
      const collection = await col.setArticles([])
      ctx.body = {
        code: '0',
        collection: collection,
        desc: '已清空'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '清空失败'
      }
    }
  }

  // 编辑收藏夹
  async editCollection() {
    try {
      const { ctx } = this
      const { id, collectionForm } = ctx.request.body
      const collection = await ctx.model.Collection.update(collectionForm, {
        where: {
          id
        }
      })
      ctx.body = {
        code: '0',
        collection: collection,
        desc: '编辑成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '编辑失败',
        err: error
      }
    }
  }

  // 删除收藏夹
  async deleteCollection() {
    try {
      const { ctx } = this
      const { id } = ctx.request.body
      const collection = await ctx.model.Collection.destroy({
        where: {
          id
        }
      })
      ctx.body = {
        code: '0',
        collection: collection,
        desc: '删除成功'
      }
    } catch(error) {
      const { ctx } = this
      ctx.body = {
        code: '-1',
        desc: '删除失败',
        err: error
      }
    }
  }
}

module.exports = CollectionController